package reader;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ScrabbleDictionnary implements DicoReader{
	
	private static final String DICO_PATH = "dico2.ret";
	private BufferedReader mReader;
	public static final String WILDCARD = "[a-zA-Z]";
	
	ArrayList<String> mDico = new ArrayList<String>();
	
	public ScrabbleDictionnary()
	{
		openFile();
		String s = "";
		try {
			while((s = mReader.readLine()) != null)
			{
				mDico.add(s);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		closeFile();
	}
	
	private void openFile()
	{
		 try {
				File file = new File(DICO_PATH);
				FileInputStream fis = new FileInputStream(file);
			    BufferedInputStream bis = new BufferedInputStream(fis);
			    mReader = new BufferedReader(new InputStreamReader(bis));
			    mReader.mark(bis.available());
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	private void closeFile()
	{
		try {
			mReader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<String> getMatchPattern(String pattern) {
		List<String> matchPattern = new ArrayList<String>();
	
		for(String s : mDico)
		{
				if(s.matches(pattern))
					matchPattern.add(s);
		}
		
		return matchPattern;
	}

	@Override
	public boolean hasAtLeastOnMatch(String pattern) {
		pattern = pattern.toUpperCase();
		for(String s : mDico)
		{
			if(s.matches(pattern))
			{	
				return true;
			}
		}
		return false;
	}

	@Override
	public String getWildCard() {
		return WILDCARD;
	}

	@Override
	//TODO: do a random
	public String getOneMatchPattern(String pattern) {
		for(String s : mDico)
		{
			if(s.matches(pattern))
				return s;
		}
		return "";
	}

	@Override
	public List<String> takeMatchPattern(String pattern, int howMany) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

}
